package com.example.demo.controller;

import com.example.demo.AppUtil.BaseActionUtil;
import com.example.demo.entity.AfBrand;
import com.example.demo.entity.AfoneCategory;
import com.example.demo.entity.AfoneshopChannel;
import com.example.demo.entity.AfoneshopTopic;
import com.example.demo.entity.DTO.GoodsNewDTO;
import com.example.demo.service.*;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author:大力王
 * @Date:2024/6/21-21-6:38
 * 开始了
 */
@RestController
@Api(tags = "首页接口")
@RequestMapping("/index")
@Slf4j
public class AfIndexController extends BaseActionUtil {
    @Resource
    private IAfBrandService afBrandService;
    @Autowired
    private IAfoneshopChannelService channelService;
    @Autowired
    private IAfoneshopTopicService topicService;
    @Autowired
    private IAfoneCategoryService afoneCategoryService;
    @Autowired
    private IGoodsService goodsService;

    @ApiOperation(value = "专题精选")
    @ApiOperationSupport(author = "Mr.力铉器")
    @PostMapping("/topic")
    public Object indexImgs() {
        /*
          初始化结果对象，用于存储查询到的专题列表和其他可能的结果信息。
         */
        Map<String, Object> resultObj = new HashMap<String, Object>();

        /*
          初始化查询参数对象，用于指定查询专题列表时的条件。
         */
        Map<String, Object> param = new HashMap<String, Object>();

        /*
          设置查询参数，指定查询的起始位置，这里使用0表示从第一条数据开始查询。
         */
        param.put("offset", 0);

        /*
          设置查询参数，指定每页查询的数据条数，这里设置为3，表示每页查询3条专题数据。
         */
        param.put("limit", 3);

        /*
          调用专题服务的查询方法，根据参数查询专题列表。
          @param param 查询参数，包含分页信息。
         * @return 返回查询到的专题列表。
         */
        List<AfoneshopTopic> topicList = topicService.queryList(param);
        /*
          将查询到的专题列表存储到结果对象中，以便后续处理或返回给调用方。
         */
        resultObj.put("topicList", topicList);


        return toResponseSuccess(resultObj);
    }
    @ApiOperation(value = "品牌制造商")
    @ApiOperationSupport(author = "Mr.力铉器")
    @PostMapping("/brand")
    public Object brand() {
        // 初始化一个空的HashMap，用于存储最终的结果对象
        Map<String, Object> resultObj = new HashMap<String, Object>();

        // 初始化一个参数映射，用于存储查询品牌的参数
        Map<String, Object> param = new HashMap<String, Object>();

        // 设置参数：标记为新品牌
        param.put("is_new", 1);
        // 设置参数：排序字段为新排序顺序
        param.put("sidx", "new_sort_order ");
        // 设置参数：排序方式为升序
        param.put("order", "asc ");
        // 设置参数：查询起始偏移量为0
        param.put("offset", 0);
        // 设置参数：查询限制数量为4
        param.put("limit", 4);

        // 调用afBrandService的queryList方法，查询符合参数条件的品牌列表
        List<AfBrand> brandList = afBrandService.queryList(param);

        // 将查询到的品牌列表放入结果对象中
        resultObj.put("brandList", brandList);


        return toResponseSuccess(resultObj);
    }
    @ApiOperation(value = "快捷类目")
    @ApiOperationSupport(author = "Mr.力铉器")
    @PostMapping("/channel")
    public Object channel() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param;
        param = new HashMap<>();
        param.put("sidx", "sort_order ");
        param.put("order", "asc ");
        List<AfoneshopChannel> channel = channelService.queryList(param);
        resultObj.put("channel", channel);
        //

        return toResponseSuccess(resultObj);
    }
    @ApiOperation(value = "商品分类及分类下的商品")
    @ApiOperationSupport(author = "Mr.力铉器")
    @PostMapping("/category")
    public Object categoryAndGoods() {
        Map<String, Object> resultObj = new HashMap<String, Object>();
        //
        Map<String, Object> param = new HashMap<String, Object>();
        param = new HashMap<String, Object>();
        param.put("parentId", 0);
        param.put("notName", "推荐");//<>
        List<AfoneCategory> categoryList = afoneCategoryService.queryList(param);
        List<Map<String, Object>> newCategoryList = new ArrayList<>();

        for (AfoneCategory categoryItem : categoryList) {
            param.remove("fields");
            param.put("parentId", categoryItem.getId());
            List<AfoneCategory> categoryEntityList = afoneCategoryService.queryList(param);
            List<Integer> childCategoryIds = null;
            if (categoryEntityList != null && !categoryEntityList.isEmpty()) {
                childCategoryIds = new ArrayList<>();
                for (AfoneCategory categoryEntity : categoryEntityList) {
                    childCategoryIds.add(categoryEntity.getId());
                }
            }
            //
            param = new HashMap<>();
            param.put("categoryIds", childCategoryIds);
            param.put("fields", "id as id, name, list_pic_url, retail_price as retail_price");
            param.put("isDelete", "0");
            PageHelper.startPage(0, 7, false);
            List<GoodsNewDTO> categoryGoods = goodsService.queryList(param);
            log.info("categoryGoods:{}", categoryGoods);
            Map<String, Object> newCategory = new HashMap<String, Object>();
            newCategory.put("id", categoryItem.getId());
            newCategory.put("name", categoryItem.getName());
            newCategory.put("goodsList", categoryGoods);
            newCategoryList.add(newCategory);
        }
        resultObj.put("categoryList", newCategoryList);
        //

        return toResponseSuccess(resultObj);
    }
}
